// subcontract/Home/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
      name: '找产品',
      type: 0
    }, {
      name: '找厂家',
      type: 1
    }],
    cartList: [],//购物车列表
    all: null,//全选状态
    cartinfo: {},
    activeIndex: 0
  },
  getSelectCart() {
    let {
      cartList
    } = this.data;
    return cartList.reduce((pre, item) => {
      return pre.concat(item.cart.filter(i => i.selected && !this.cartInvalid(i)).map(i => i
        .cart_id))
    }, [])
  },
  cartInvalid(item) {
    return item.goods_status == 0 || item.goods_del == 1 || item.is_pay == 0 || item.has_item == 0 ? true : false || item.stock == 0
  },
  confirmOrder() {
    let {
      cartList
    } = this.data;
    let goods = [];
    let carts = this.getSelectCart()
    console.log(carts);
    if (carts.length == 0) return getApp().showToast('您还没有选择商品哦')
    // 处理出商品数组数据
    cartList.forEach(item => {
      if (item.cart.length != 0) {
        item.cart.forEach((el, i) => {
          // 选中的商品才能进入
          if (el.selected == 1) {
            goods.push({
              item_id: el.item_id,
              num: el.goods_num,
              goods_id: el.goods_id,
              shop_id: item.shop.shop_id,
              delivery_type: 0
            })
          }
        })
      }
    })
    const params = {
      carts: carts.toString(),
      goods: JSON.stringify(goods),
      type: 'cart'
    };
    console.log(params);
    wx.navigateTo({
      url: '/subcontract/Home/confirmOrder/index',
      success: function (res) {
        res.eventChannel.emit('confirmOrder', params)
      }
    })
  },
  /** 跳转 */
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    console.log(url);
    wx.navigateTo({
      url,
    })
  },
  /**删除购物车商品 */
  async cartDel(event) {
    let cart_id = event.currentTarget.dataset.id
    let object = {
      cart_id
    }
    const ret = await wx.showModal({
      title: '提示',
      content: '您确定要删除此商品嘛？',
    })
    if (ret.confirm) {
      const { code, data } = await getApp().api.cartDel(object)
      if (code == 1) {
        this.cartLists()
      }
    }
  },
  // 判断全选状态
  isSelectedAll() {
    let cartLists = this.data.cartList
    if (!cartLists.length) return false
    let index = cartLists.findIndex(item => item.is_selected == 0);
    this.setData({
      all: index == -1 ? true : false
    })
  },
  /**选中店铺 */
  async selected(event) {
    console.log(event);
    let { index, selected, type, id } = event.currentTarget.dataset
    let cartLists = this.data.cartList
    let cartId = []
    switch (type) {
      case '1':
        cartId = cartLists[index].cart.map(item => item.cart_id)
        break;
      case '2':
        cartId.push(id)
        console.log(id);
        break;
      case '3':
        cartId = cartLists.reduce((pre, item) => {
          return pre.concat(item.cart.map(i => i.cart_id))
        }, cartId)
        break;
    }
    let object = {
      cart_id: cartId,
      selected: selected == 0 ? 1 : 0
    }
    const { code, data } = await getApp().api.selected(object)
    if (code == 1) {
      this.cartLists()
    }
  },
  /**购物车列表 */
  async cartLists() {
    let { activeIndex } = this.data
    let object = {
      type: activeIndex
    }
    const { code, data } = await getApp().api.cartLists(object)
    if (code == 1) {
      this.setData({
        cartList: data.lists,
        cartinfo: data
      })
      this.isSelectedAll()
    }
  },
  /**onChangeTabs切换 */
  onChangeTabs(event) {
    console.log(event);
    this.setData({ activeIndex: event.detail.index })
    this.cartLists()
  },
  /**步进气回调 */
  async onChange(event) {
    console.log(event);
    let cart_id = event.currentTarget.dataset.id
    let num = event.detail
    let object = {
      cart_id,
      goods_num: num
    }
    const { code, data } = await getApp().api.cartChange(object)
    if (code == 1) {
      this.cartLists()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.cartLists()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})